/*
 * @ProjectName: 编程学习
 * @Copyright:   2020 HangZhou Yiyuery Dev, Ltd. All Right Reserved.
 * @address:     微信搜索公众号「架构探险之道」获取更多资源。
 * @date:        2020/9/6 11:28 上午
 * @blog:         https://yiyuery.blog.csdn.net/
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package pub.dsb.api.controller.decrypt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pub.dsb.framework.boot.rest.R;
import pub.dsb.framework.boot.security.annotation.Decrypt;
import pub.dsb.framework.boot.security.beans.strategy.DesDecryptStrategy;
import pub.dsb.framework.boot.security.beans.strategy.RsaDecryptStrategy;
import pub.dsb.framework.boot.security.constants.DecryptTypeEnum;

import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * <p>
 *  加解密功能测试
 * </p>
 *
 * @author Yiyuery
 * @date 2020/9/6 11:28 上午
 */
@RequestMapping("/des")
@RestController()
public class DESTestController {

    @Autowired
    private DesDecryptStrategy desDecryptStrategy;

    /**
     * 动态解密
     * @param value
     * @return
     */
    @PostMapping("/auto")
    @Decrypt(DecryptTypeEnum.DES)
    public R<String> postDecryptAuto(@RequestBody String value){
        return R.ok(value);
    }

    /**
     * 手动解密 + URL DECODE
     * @param value
     * @return
     * @throws Exception
     */
    @PostMapping("/hand/ud")
    public R<String> postDecryptHand(String value) throws Exception {
            return R.ok(desDecryptStrategy.decrypt(URLDecoder.decode(value).replaceAll(" ","+")));
    }
    /**
     * 手动加密 + URL ENCODE
     * @param value
     * @return
     * @throws Exception
     */
    @PostMapping("/hand/ue")
    public R<String> postDecryptHandWithUE(String value) throws Exception {
            return R.ok(URLEncoder.encode(desDecryptStrategy.encrypt(value)));
    }

    /**
     * 手动加密
     * @param value
     * @return
     * @throws Exception
     */
    @PostMapping("/encrypt")
    public R<String> postEncryptHand(String value) throws Exception {
        return R.ok(desDecryptStrategy.encrypt(value));
    }
}
